package register;

import java.sql.*;
import java.util.Properties;
import java.io.*;

public class JdbcTools {
	/**
	 * @连接数据库
	 */
	public static Connection getConn() throws Exception {
		Properties p = new Properties();
		FileInputStream fis = new FileInputStream(
				"../../profile/mysql.properties");
		p.load(fis);
		fis.close();
		String driverName = p.getProperty("driverName");
		String url = p.getProperty("url");
		String userName = p.getProperty("userName");
		String pwd = p.getProperty("pwd");
		Connection con = null;
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url, userName, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	/**
	 * @关闭数据库
	 */
	public static void close(ResultSet rs, Statement stmt, Connection con) {
		try {
			if (rs != null)
				rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (stmt != null)
				stmt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (con != null)
				con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @将String类型的数字转换成int类型的数字
	 */
	private static Integer toInt(String num) {
		Integer i = null;
		try {
			i = Integer.parseInt(num);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	/**
	 * @打印方法，将得到的表内容打印出来；
	 */
	public static StringBuffer printRs(ResultSet rs) {
		StringBuffer sb = new StringBuffer();
		try {
			ResultSetMetaData meta = rs.getMetaData();
			int cols = meta.getColumnCount();
			while (rs.next()) {
				for (int i = 1; i <= cols; i++) {
					sb.append(meta.getColumnName(i) + ":");
					sb.append(rs.getString(i) + " ");
				}
				sb.append("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb;
	}

}
